/*
 * AppAction.java July 2009
 *
 * Copyright (C) 2009, Quirino Zagarese <quirino@laszloitalia.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 */
package org.eu4ria;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eu4ria.exceptions.IllegalParameterValueException;
import org.eu4ria.parameters.ParameterValidator;

/**
 * The Class OWAction.
 */
public class AppAction {


	private String hash;

	/** The parameters. */
	private List<ParameterValidator> parameters = new ArrayList<ParameterValidator>();

	/** The java method. */
	private Method javaMethod;

	/** The declaring module. */
	private AppModule declaringModule;


	/** The signature. */
	private String signature;

	/**
	 * Gets the signature.
	 *
	 * @return the signature
	 */
	public String getSignature() {
		return signature;
	}

	/**
	 * Gets the java method.
	 *
	 * @return the java method
	 */
	public Method getJavaMethod() {
		return javaMethod;
	}

	/**
	 * Sets the java method.
	 *
	 * @param m the new java method
	 */
	public void setJavaMethod(Method m) {
		this.javaMethod = m;
		createSignature();
	}

	/**
	 * Gets the declaring module.
	 *
	 * @return the declaring module
	 */
	public AppModule getDeclaringModule() {
		return declaringModule;
	}

	/**
	 * Sets the declaring module.
	 *
	 * @param module the new declaring module
	 */
	public void setDeclaringModule(AppModule module) {
		this.declaringModule = module;
	}


	/**
	 * Gets the parameters.
	 *
	 * @return the parameters
	 */
	public List<ParameterValidator> getParameters() {
		return parameters;
	}

	/**
	 * Adds the parameter.
	 *
	 * @param parameter the parameter
	 */
	public void addParameter(ParameterValidator parameter){
		parameter.setDeclaringAction(this);
		parameters.add(parameter);
	}


	public ActionResult execute(Map map){
		ActionResult result = new ActionResult();
		Object object = declaringModule.getJavaObject();
		//Method[] methods = object.getClass().getDeclaredMethods();
		Object[] params = new Object[this.parameters.size()];
		Class[] types = this.javaMethod.getParameterTypes();
		int i = 0;
		int size = parameters.size();
		for(i = 0; i<size; i++){
			ParameterValidator parameter = parameters.get(i);
			String[] param = (String[]) map.get("p" + i);
			try {
				if(param!=null){
					parameter.acceptsValue(param[0],types[i]);
					params[i] = parameter.getJavaValue(param[0]);
				}
			} catch (IllegalParameterValueException e) {
				result.setErrorMessage(e.getMessage());
				result.setErrorStackTrace(e.getStackTrace());
				result.setParamErrorNum(i);
				return result;
			}
		}
		result.setType(javaMethod.getReturnType());
		try {
			result.setValue(javaMethod.invoke(object, params));
		} catch (Exception e) {
			result.setErrorMessage(e.getCause().getMessage());
			result.setErrorStackTrace(e.getCause().getStackTrace());
			return result;
		}
		return result;
	}

	private void createSignature(){
		String signature = this.javaMethod.getName() + "(";
		Class[] parameterTypes;
		parameterTypes = javaMethod.getParameterTypes();
		for (int i = 0; i < parameterTypes.length; i++) {
			Class c = parameterTypes[i];
			signature = signature + c.getName();
			if (i < (parameterTypes.length - 1)) {
				signature = signature + ",";
			}
		}
		signature = signature + ")";
		this.signature = signature;
	}

	public String getHash() {
		return hash;
	}

	public void setHash(String hash) {
		this.hash = hash;
	}
}
